/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.admin.controller;

import com.jiantong.user.model.DeclareStation;
import com.jiantong.user.model.Department;
import com.jiantong.user.service.DepartmentWriteService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.exception.JsonResponseException;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : panxin
 */
@Slf4j
@RestController
@RequestMapping
public class AdminDepartments {

    @RpcConsumer
    private DepartmentWriteService departmentWriteService;

    /**
     * 运营创建政府下的部门信息
     *
     * @param department 部门信息
     * @return 创建结果
     */
    @RequestMapping(value = "/api/department/create-department", method = RequestMethod.POST)
    public Boolean createDepartment(@RequestBody Department department) {
        Response<Boolean> resp = departmentWriteService.createNormalDepartment(department);
        if (!resp.isSuccess()) {
            log.error("failed to create normal department = {}, cause : {}", department, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    /**
     * 运营创建申报点
     *
     * @param station 申报点信息
     * @return 创建结果
     */
    @RequestMapping(value = "/api/department/create-declare-station", method = RequestMethod.POST)
    public Boolean createDeclareStation(@RequestBody DeclareStation station) {
        checkLocation(station.getProvinceId(), station.getCityId(), station.getRegionId());
        Response<Boolean> resp = departmentWriteService.createDeclareStation(station);
        if (!resp.isSuccess()) {
            log.error("failed to create declare station = {}, cause : {}", station, resp.getError());
            throw new JsonResponseException(resp.getError());
        }
        return resp.getResult();
    }

    private void checkLocation(Long provinceId, Long cityId, Long regionId) {
        if (provinceId == null) {
            log.error("province id is null");
            throw new JsonResponseException("province.id.is.null");
        }
        if (cityId == null) {
            log.error("city id is null");
            throw new JsonResponseException("city.id.is.null");
        }
        if (regionId == null) {
            log.error("region id is null");
            throw new JsonResponseException("region.id.is.null");
        }
    }

}
